package com.tiger.admin.http.teacher;

import org.apache.commons.codec.binary.Hex;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

@Service
public class TeacherService {
    public String buildPassword(String password, String salt) {
        password = sha256Hash(password);
        return sha256Hash(password + salt);
    }

    public String generateSalt() {
        // 生成标准形式的UUID
        String[] uuids = UUID.randomUUID().toString().split("-");


        return uuids[uuids.length - 1];
    }

    public static String sha256Hash(String input) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hashBytes = digest.digest(input.getBytes(StandardCharsets.UTF_8));
            return Hex.encodeHexString(hashBytes).toLowerCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }
}
